﻿using Api.Common;
using Api.Model.Dto.ArticleDto;
using Api.Model.Model;
using Api.Services;
using Api.StartUpConfig;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace Api.AutoMapper.ArticleResolver
{
    public class UpdateArticleUrlResolver : IValueResolver<UpdateArticleInput, Article, string>
    {
        private readonly ArticleServices _articleServices;

        public UpdateArticleUrlResolver(ArticleServices articleServices)
        {
            _articleServices = articleServices;
        }
        public string Resolve(UpdateArticleInput source, Article destination, string destMember, ResolutionContext context)
        {
            var article =  _articleServices.SingleAsync(x => x.Id.Equals(destination.Id)).Result;
            File.WriteAllText(StaticInfo.WebRootPath + "/Article/" + article.Url, source.Body);
            return article.Url;
        }
    }
}
